package com.mes.control.provider;

import com.mes.common.framework.Exception.DubboProviderException;
import com.mes.common.framework.rest.view.Page;
import com.mes.common.utils.IDGenerator;
import com.mes.control.mapper.SalseOrderMapper;
import com.mes.control.utils.SalseOrderNumUntil;
import com.mes.dubbo.interprovider.control.ISalseOrderProvider;
import com.mes.entity.control.CountOrderDetailInfo;
import com.mes.entity.control.SalseOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.persistence.Id;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by leden on 2017/12/4.
 */
public class SalseOrderProviderImpl extends BaseProviderImpl<SalseOrder>implements ISalseOrderProvider {
    @Autowired
    @Qualifier("salseOrderMapper")
    private SalseOrderMapper salseOrderMapper;
    @Override
    public SalseOrderMapper getBaseInterfaceMapper(){return salseOrderMapper;}

    public String saveSalseOrder(SalseOrder salseOrder)throws DubboProviderException{
        List<SalseOrder> list = salseOrderMapper.findAll();
        String id= IDGenerator.getID();
        int count=0;
        if(list !=null) {
             count= list.size();
        }
        Date date = new Date();
        String orderNum = SalseOrderNumUntil.getOrderNum(date,count);
        salseOrder.setOrderNum(orderNum);
        salseOrder.setId(id);
        salseOrderMapper.save(salseOrder);
         return id;
    }
   public Page countOrder(Page page, Map<String,Object> map) throws DubboProviderException{
       Page pageLoad = page;
       pageLoad.setTotal(1);
       //统计总订单，及总订单金额
       List<SalseOrder> list = salseOrderMapper.findAll();
       int allAmount =0;
       if(list !=null){
           for (int i=0;i<list.size();i++){
               allAmount+=Integer.parseInt(list.get(i).getAmount());
           }
       }
       String orderToalAmount=String.valueOf(allAmount);
       String orderToalNumber = String.valueOf(salseOrderMapper.getCount(map));
       //统计有效订单，及有效订单金额
       map.put("status","1");
       String orderValidNumber =String.valueOf(salseOrderMapper.getCount(map));
       int validAmount =0;
       List<SalseOrder> list1 = salseOrderMapper.findByMap(map);
       if(list1 !=null){
           for (int i=0;i<list1.size();i++){
               validAmount +=Integer.parseInt(list1.get(i).getAmount());
           }
       }
       String orderValidAmount = String.valueOf(validAmount);
       //统计退单数及退单金额
       map.put("status","0");
       String chargebackOrderNumber =String.valueOf(salseOrderMapper.getCount(map));
       int chargebackAmount=0;
       List<SalseOrder>list2 = salseOrderMapper.findByMap(map);
       if(list2 !=null){
           for (int i=0;i<list2.size();i++){
               chargebackAmount +=Integer.parseInt(list2.get(i).getAmount());
           }
       }
       String chargeOrderAmount = String.valueOf(chargebackAmount);
       CountOrderDetailInfo countOrderDetailInfo =new CountOrderDetailInfo();
       countOrderDetailInfo.setOrderToalNumber(orderToalNumber);
       countOrderDetailInfo.setOrderToalAmount(orderToalAmount);
       countOrderDetailInfo.setOrderValidNumber(orderValidNumber);
       countOrderDetailInfo.setOrderValidAmount(orderValidAmount);
       countOrderDetailInfo.setChargebackOrderNumber(chargebackOrderNumber);
       countOrderDetailInfo.setChargeOrderAmount(chargeOrderAmount);
       List<CountOrderDetailInfo> list3 = new ArrayList<>();
       list3.add(countOrderDetailInfo);
       pageLoad.setRows(list3);
       return pageLoad;

    }

   public boolean updateChargeBackOrder(String id)throws DubboProviderException{
        SalseOrder salseOrder = new SalseOrder();
        salseOrder.setId(id);
        salseOrder.setStatus("0");
         salseOrderMapper.update(salseOrder);
         return true;
    }
}
